<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Noto Serif SC:300,300italic,400,400italic,700,700italic|Roboto Mono:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-bounce.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"zhiruozzy.cn","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"default"},"back2top":{"enable":"truw","sidebar":true,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="CSS中的一些重点知识总结 ​                  ——选择器、行内元素和块级元素、定位">
<meta property="og:type" content="article">
<meta property="og:title" content="CSS自学笔记">
<meta property="og:url" content="http://zhiruozzy.cn/2022/04/02/CSS/index.html">
<meta property="og:site_name" content="芷若">
<meta property="og:description" content="CSS中的一些重点知识总结 ​                  ——选择器、行内元素和块级元素、定位">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://zhiruozzy.cn/img/CSS/image-20220403091930338.png">
<meta property="og:image" content="http://zhiruozzy.cn/img/CSS/image-20220403095644730.png">
<meta property="og:image" content="http://zhiruozzy.cn/img/CSS/image-20220403095836903.png">
<meta property="og:image" content="http://zhiruozzy.cn/img/CSS/image-20220403095644730.png">
<meta property="og:image" content="http://zhiruozzy.cn/img/CSS/image-20220403100110669.png">
<meta property="article:published_time" content="2022-04-02T04:10:54.000Z">
<meta property="article:modified_time" content="2022-04-07T14:08:01.665Z">
<meta property="article:author" content="芷若">
<meta property="article:tag" content="前端">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://zhiruozzy.cn/img/CSS/image-20220403091930338.png">

<link rel="canonical" href="http://zhiruozzy.cn/2022/04/02/CSS/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>CSS自学笔记 | 芷若</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<!-- hexo injector head_end start -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css">

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-math@4.0.0/dist/style.css">
<!-- hexo injector head_end end --><link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css">
<link rel="stylesheet" href="/css/prism-line-numbers.css" type="text/css"></head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">芷若</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-主页">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>主页</a>

  </li>
        <li class="menu-item menu-item-关于">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-标签">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-目录">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>目录</a>

  </li>
        <li class="menu-item menu-item-归档">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container"></div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="algolia-results">
  <div id="algolia-stats"></div>
  <div id="algolia-hits"></div>
  <div id="algolia-pagination" class="algolia-pagination"></div>
</div>

      
    </div>
  </div>

</div>
    </header>

    
  <div class="reading-progress-bar"></div>

  <a href="https://github.com/zhiruozzy" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://zhiruozzy.cn/2022/04/02/CSS/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/touxiang.jpg">
      <meta itemprop="name" content="芷若">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="芷若">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          CSS自学笔记
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2022-04-02 12:10:54" itemprop="dateCreated datePublished" datetime="2022-04-02T12:10:54+08:00">2022-04-02</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2022-04-07 22:08:01" itemprop="dateModified" datetime="2022-04-07T22:08:01+08:00">2022-04-07</time>
              </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>2.5k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>2 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>CSS中的一些重点知识总结</p>
<p>​                  ——选择器、行内元素和块级元素、定位</p>
<span id="more"></span>

<h2 id="选择器"><a href="#选择器" class="headerlink" title="选择器"></a>选择器</h2><p>功能：实现对HTML中元素的一对一，一对多或者多对一的控制</p>
<h3 id="元素选择器（element）："><a href="#元素选择器（element）：" class="headerlink" title="元素选择器（element）："></a>元素选择器（element）：</h3><ul>
<li><p>语法：元素名称{属性：属性值;}</p>
</li>
<li><p>实例：</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">div</span>&#123;</span><br><span class="line">   <span class="attribute">width</span>:<span class="number">100px</span>;</span><br><span class="line">   <span class="attribute">height</span>:<span class="number">200px</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>缺点：使用元素选择器时会对所有元素进行选择，所以操作的时候会不方便</p>
</li>
</ul>
<h3 id="类选择器（class）："><a href="#类选择器（class）：" class="headerlink" title="类选择器（class）："></a>类选择器（class）：</h3><ul>
<li><p>语法：**.**class名{属性：属性值；}</p>
</li>
<li><p>说明：一个class名可以给很多元素定义（一对多），通过class名来控制你想控制的一类元素。</p>
</li>
<li><p>示例：</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&lt;<span class="selector-tag">div</span> class=&quot;box&quot;&gt; &lt;/<span class="selector-tag">div</span>&gt;</span><br><span class="line">&lt;style&gt;</span><br><span class="line">  <span class="selector-class">.box</span>&#123;</span><br><span class="line">     <span class="attribute">width</span>:<span class="number">200px</span>;</span><br><span class="line">     <span class="attribute">height</span>:<span class="number">200px</span>;</span><br><span class="line">     &#125;</span><br><span class="line">&lt;/style&gt;</span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="id选择器"><a href="#id选择器" class="headerlink" title="id选择器"></a>id选择器</h3><ul>
<li>语法：#id名{属性名：属性值}</li>
<li>说明：一个id名只能对应HTML中一个具体的元素（唯一性），在css中不常用，通常在JS中用（getElementById)</li>
</ul>
<h3 id="选择器（通配符选择器）"><a href="#选择器（通配符选择器）" class="headerlink" title="*选择器（通配符选择器）"></a>*选择器（通配符选择器）</h3><p>一般用于最开始清除所有元素的默认页边距和填充值</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">*&#123;</span><br><span class="line"><span class="attribute">padding</span>:<span class="number">0</span>;</span><br><span class="line"><span class="attribute">margin</span>:<span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="群组选择器"><a href="#群组选择器" class="headerlink" title="群组选择器"></a>群组选择器</h3><p>语法：选择符1，选择符2，选择符3..{属性：属性值}</p>
<p>示例：</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">h1</span>,<span class="selector-tag">h2</span>,<span class="selector-tag">h3</span>,<span class="selector-tag">h4</span>&#123;</span><br><span class="line">   <span class="attribute">color</span>:<span class="number">#369</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="包含选择器（后代选择器）"><a href="#包含选择器（后代选择器）" class="headerlink" title="包含选择器（后代选择器）"></a>包含选择器（后代选择器）</h3><p>示例：</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">span</span> <span class="selector-tag">p</span> <span class="selector-class">.box</span>&#123;</span><br><span class="line">   <span class="attribute">color</span>:<span class="number">#369</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>用空格分开（没有逗号），选择的是span中的p标签中类名为box的元素</p>
<h2 id="块级元素和行内元素"><a href="#块级元素和行内元素" class="headerlink" title="块级元素和行内元素"></a>块级元素和行内元素</h2><h3 id="块级元素"><a href="#块级元素" class="headerlink" title="块级元素"></a>块级元素</h3><p>自己一个人在浏览器中占据整行，即排斥与其他元素位于同一行，如果不设置宽度，默认是100%</p>
<table>
<thead>
<tr>
<th>种类</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>div</td>
<td>最典型的块元素</td>
</tr>
<tr>
<td>p</td>
<td>表示段落</td>
</tr>
<tr>
<td>h1-h6</td>
<td>表示1-6级标题（默认加粗）</td>
</tr>
<tr>
<td>br</td>
<td>表示换行</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="http://c.biancheng.net/view/7534.html">ol</a></td>
<td>有序列表</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="http://c.biancheng.net/view/7534.html">ul</a></td>
<td>无序列表</td>
</tr>
</tbody></table>
<h3 id="行内元素"><a href="#行内元素" class="headerlink" title="行内元素"></a>行内元素</h3><p>可以与其他行内元素共占一行，只有当多个行内元素的总宽度大于浏览器的宽度的时候，才会换行显示。</p>
<table>
<thead>
<tr>
<th>行内元素</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td><a target="_blank" rel="noopener" href="http://c.biancheng.net/view/7529.html">a</a></td>
<td>超链接</td>
</tr>
<tr>
<td>span</td>
<td>常用行级</td>
</tr>
<tr>
<td>b</td>
<td>加粗，不强调</td>
</tr>
<tr>
<td>em</td>
<td>斜体，强调</td>
</tr>
<tr>
<td>i</td>
<td>斜体，不强调</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="http://c.biancheng.net/view/7528.html">img</a></td>
<td>图片</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="http://c.biancheng.net/view/7574.html">input</a></td>
<td>输入框</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="http://c.biancheng.net/view/7613.html">select</a></td>
<td>下拉列表</td>
</tr>
</tbody></table>
<h2 id="定位"><a href="#定位" class="headerlink" title="定位"></a>定位</h2><h3 id="分类"><a href="#分类" class="headerlink" title="分类"></a>分类</h3><p>通过css中的<strong>position</strong>属性来设置</p>
<table>
<thead>
<tr>
<th>值</th>
<th>语义</th>
</tr>
</thead>
<tbody><tr>
<td>static</td>
<td>静态定位</td>
</tr>
<tr>
<td>relative</td>
<td>相对定位</td>
</tr>
<tr>
<td>absolute</td>
<td>绝对定位</td>
</tr>
<tr>
<td>fixed</td>
<td>固定定位</td>
</tr>
</tbody></table>
<h3 id="边偏移"><a href="#边偏移" class="headerlink" title="边偏移"></a>边偏移</h3><p>边偏移用于对盒子的定位，可以设置<strong>top,bottom,left,right四个属性</strong>，分别代表<strong>距离父元素</strong>上下左右的边线的距离</p>
<h3 id="相对定位（relative）"><a href="#相对定位（relative）" class="headerlink" title="相对定位（relative）"></a>相对定位（relative）</h3><p>特点：</p>
<ul>
<li>相对定位是相对于他原来的位置来说的（自恋型），即以原来盒子的左上角建系，设置的边偏移都是以这个坐标系移动</li>
<li>相对定位相对自己移动了之后，原来的位置依旧保留，跟前的盒子不会挤占他的位置</li>
</ul>
<p>特点：</p>
<h3 id="绝对定位（absolute）"><a href="#绝对定位（absolute）" class="headerlink" title="绝对定位（absolute）"></a>绝对定位（absolute）</h3><p>注意：绝对定位是相对于他的父元素来说的。（拼爹型）</p>
<p>语法：<code>选择器&#123; position: absolute; &#125;</code></p>
<p>特点：</p>
<ul>
<li>如果<strong>没有父元素</strong>或者<strong>父元素没有定位</strong>（是个标准流或者是个浮动的盒子），则以document浏览器为准来定位</li>
<li>如果祖先元素有定位（绝对，相对，固定定位都行），则以最近一级的有定位的祖先元素为参考点移动位置</li>
<li>绝对定位不再占用原来的位置（脱标）。（比如轮播图中的左右箭头不占有位置，不会挤到下面的轮播图片）</li>
</ul>
<h3 id="子绝父相"><a href="#子绝父相" class="headerlink" title="子绝父相"></a>子绝父相</h3><p>典型案例：轮播图</p>
<p>首先一个大的div，包裹轮播图片，左右按钮和小圆点，用的是相对定位。而左右按键和下面的小圆点都是子元素，用的是绝对定位。</p>
<p>特点：</p>
<ul>
<li>子级绝对定位，不会占有位置，可以放到父盒子里面的任何一个地方，不会影响其他的兄弟</li>
<li>父级要定位，这样才能让子元素相对于父元素进行移动，而不是相对于浏览器移动。但不能也是绝对定位，因为如果也是绝对定位，那么父元素也不占有位置，他跟前的盒子就会补充他的位置，跑到他的下面去，造成结构混乱。所以只能是相对定位</li>
</ul>
<h3 id="固定定位（fixed）"><a href="#固定定位（fixed）" class="headerlink" title="固定定位（fixed）"></a>固定定位（fixed）</h3><p>作用：可以固定到浏览器可视区的一个固定位置，在页面滚动的时候元素的位置不会改变（比如看板娘，两侧固定的广告，返回顶部的按钮）</p>
<p>语法：<code>position：fixed</code></p>
<p>注意：</p>
<ul>
<li>他的位置是参照浏览器的<strong>可视窗口</strong></li>
<li>固定定位不占有原来的位置（脱标）</li>
<li>与父元素没有任何关系</li>
</ul>
<p>典型案例：返回顶部的按钮</p>
<p>这个按钮始终在版心的右侧，即<img src="/../img/CSS/image-20220403091930338.png" alt="image-20220403091930338"></p>
<p>算法：</p>
<p>首先让按钮向右走浏览器的一半，即<code>left:50%</code>,再向右走版心宽度的一半，因为left已经设置过了，所以继续移动可以设置外边距，即<code>margin-left:版心宽度的一半</code></p>
<h3 id="定位叠放次序"><a href="#定位叠放次序" class="headerlink" title="定位叠放次序"></a>定位叠放次序</h3><p>可能会出现层层重叠的情况，通过z-index来控制盒子的前后顺序（即盒子在z轴的位置）</p>
<ul>
<li>数值：负整数，0或正整数，数值越大，盒子越靠上</li>
<li>如果一系列盒子的位置和定位方式相同，没有设置z-index，则在最上面的是最后创建的那个</li>
<li>语法：选择器{z-index:2};</li>
<li>注意：①没有单位②只有定位的盒子才有这个属性，而标准流和浮动没有这个属性</li>
</ul>
<h3 id="实现居中"><a href="#实现居中" class="headerlink" title="实现居中"></a>实现居中</h3><h4 id="水平居中"><a href="#水平居中" class="headerlink" title="水平居中"></a>水平居中</h4><p>没有加定位的盒子可以通过：margin ：0 auto来实现水平居中。</p>
<p>但是加了绝对定位的盒子通过上述方法无法实现水平居中。可以通过下面的方法实现：</p>
<ol>
<li>先向右移父元素宽度的一半：left：50%</li>
<li>再往左走自己宽度的一半：margin-left：（-1）*自己宽度的一半</li>
</ol>
<h4 id="垂直居中"><a href="#垂直居中" class="headerlink" title="垂直居中"></a>垂直居中</h4><p>加了绝对定位的盒子：</p>
<ol>
<li>先向下走父元素高度的一半：top:50%</li>
<li>再向上走自己高度的一半：top-margin:(-1)*自己高度的一半</li>
</ol>
<h3 id="定位特性"><a href="#定位特性" class="headerlink" title="定位特性"></a>定位特性</h3><ol>
<li><p>当给行内元素添加绝对定位或者固定定位的时候，就会自动把行内元素转化为块内元素，之后就可以不转变他的display的方式就来设置高度和宽度。</p>
</li>
<li><p>块级元素如果不给高度和宽度，没添加定位之前，宽度默认的是整个浏览器的宽度，添加相对和绝对定位后，默认的是内容的大小（包裹性）</p>
</li>
<li><p>如果一个元素添加浮动或者绝对定位，就会脱标，不再占用原来的位置，后面的元素就会补充占据他的位置，那么盒子就会压住补充的元素，但两者有一点不同。</p>
<p>没有添加浮动之前</p>
<p><img src="/../img/CSS/image-20220403095644730.png" alt="image-20220403095644730"></p>
<p>添加浮动之后</p>
<p><img src="/../img/CSS/image-20220403095836903.png" alt="image-20220403095836903"></p>
<p>说明盒子虽然会压住后来补充的span，但不会遮住span里面的文字</p>
<p>没有添加定位之前</p>
<p><img src="/../img/CSS/image-20220403095644730.png" alt="image-20220403095644730"></p>
<p>添加绝对定位之后</p>
<p><img src="/../img/CSS/image-20220403100110669.png" alt="image-20220403100110669"></p>
<p>盒子压住了span里面的文字</p>
</li>
<li></li>
</ol>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E5%89%8D%E7%AB%AF/" rel="tag"># 前端</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2022/04/01/%E6%96%87%E4%BB%B6%E6%93%8D%E4%BD%9C/" rel="prev" title="文件操作">
      <i class="fa fa-chevron-left"></i> 文件操作
    </a></div>
      <div class="post-nav-item">
    <a href="/2022/04/03/%E8%BD%AE%E6%92%AD%E5%9B%BE/" rel="next" title="轮播图">
      轮播图 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%80%89%E6%8B%A9%E5%99%A8"><span class="nav-number">1.</span> <span class="nav-text">选择器</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%85%83%E7%B4%A0%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%88element%EF%BC%89%EF%BC%9A"><span class="nav-number">1.1.</span> <span class="nav-text">元素选择器（element）：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%B1%BB%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%88class%EF%BC%89%EF%BC%9A"><span class="nav-number">1.2.</span> <span class="nav-text">类选择器（class）：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#id%E9%80%89%E6%8B%A9%E5%99%A8"><span class="nav-number">1.3.</span> <span class="nav-text">id选择器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%88%E9%80%9A%E9%85%8D%E7%AC%A6%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%89"><span class="nav-number">1.4.</span> <span class="nav-text">*选择器（通配符选择器）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BE%A4%E7%BB%84%E9%80%89%E6%8B%A9%E5%99%A8"><span class="nav-number">1.5.</span> <span class="nav-text">群组选择器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8C%85%E5%90%AB%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%88%E5%90%8E%E4%BB%A3%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%89"><span class="nav-number">1.6.</span> <span class="nav-text">包含选择器（后代选择器）</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9D%97%E7%BA%A7%E5%85%83%E7%B4%A0%E5%92%8C%E8%A1%8C%E5%86%85%E5%85%83%E7%B4%A0"><span class="nav-number">2.</span> <span class="nav-text">块级元素和行内元素</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9D%97%E7%BA%A7%E5%85%83%E7%B4%A0"><span class="nav-number">2.1.</span> <span class="nav-text">块级元素</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%A1%8C%E5%86%85%E5%85%83%E7%B4%A0"><span class="nav-number">2.2.</span> <span class="nav-text">行内元素</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%9A%E4%BD%8D"><span class="nav-number">3.</span> <span class="nav-text">定位</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%86%E7%B1%BB"><span class="nav-number">3.1.</span> <span class="nav-text">分类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BE%B9%E5%81%8F%E7%A7%BB"><span class="nav-number">3.2.</span> <span class="nav-text">边偏移</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%9B%B8%E5%AF%B9%E5%AE%9A%E4%BD%8D%EF%BC%88relative%EF%BC%89"><span class="nav-number">3.3.</span> <span class="nav-text">相对定位（relative）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%9D%E5%AF%B9%E5%AE%9A%E4%BD%8D%EF%BC%88absolute%EF%BC%89"><span class="nav-number">3.4.</span> <span class="nav-text">绝对定位（absolute）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AD%90%E7%BB%9D%E7%88%B6%E7%9B%B8"><span class="nav-number">3.5.</span> <span class="nav-text">子绝父相</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9B%BA%E5%AE%9A%E5%AE%9A%E4%BD%8D%EF%BC%88fixed%EF%BC%89"><span class="nav-number">3.6.</span> <span class="nav-text">固定定位（fixed）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AE%9A%E4%BD%8D%E5%8F%A0%E6%94%BE%E6%AC%A1%E5%BA%8F"><span class="nav-number">3.7.</span> <span class="nav-text">定位叠放次序</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AE%9E%E7%8E%B0%E5%B1%85%E4%B8%AD"><span class="nav-number">3.8.</span> <span class="nav-text">实现居中</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%B0%B4%E5%B9%B3%E5%B1%85%E4%B8%AD"><span class="nav-number">3.8.1.</span> <span class="nav-text">水平居中</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%9E%82%E7%9B%B4%E5%B1%85%E4%B8%AD"><span class="nav-number">3.8.2.</span> <span class="nav-text">垂直居中</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AE%9A%E4%BD%8D%E7%89%B9%E6%80%A7"><span class="nav-number">3.9.</span> <span class="nav-text">定位特性</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="芷若"
      src="/images/touxiang.jpg">
  <p class="site-author-name" itemprop="name">芷若</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives">
          <span class="site-state-item-count">19</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
        <span class="site-state-item-count">5</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/zhiruo" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;zhiruo" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>



      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        
  <div class="beian"><a href="https://beian.miit.gov.cn/" rel="noopener" target="_blank">陕ICP备2022001874号 </a>
      <img src="http://www.beian.gov.cn/portal/download" style="display: inline-block;">
  </div>

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="芷若"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">芷若</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">105k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">1:35</span>
</div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="//cdn.jsdelivr.net/npm/algoliasearch@4/dist/algoliasearch-lite.umd.js"></script>
<script src="//cdn.jsdelivr.net/npm/instantsearch.js@4/dist/instantsearch.production.min.js"></script>
<script src="/js/algolia-search.js"></script>














  

  

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/assets/koharu.model.json"},"display":{"position":"right","width":145,"height":315},"mobile":{"show":true,"scale":0.5},"react":{"opacityDefault":0.7,"opacityOnHover":0.8},"log":false});</script></body>
</html>
